package org.apak.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import org.apak.log.FileLogger;

public class DBConnection {

	public static Connection getConnection() {
		Connection databaseConnection = null;
		String password = "";
		String user = "";
		String url = "";

		try {
			ResourceBundle bundle = ResourceBundle.getBundle("Properties.DBConnection");
			Class.forName(bundle.getString("Driver"));
			url = bundle.getString("url");
			user = bundle.getString("user");
			password = bundle.getString("password");
			databaseConnection = DriverManager.getConnection(url, user, password);

		} catch (Exception ex) {
			FileLogger.log("DBConnection.getConnection(): ", ex.getMessage());
		}
		return databaseConnection;
	}

	public static void closeConnection(final Connection connection) {
		if (connection != null) {
			try {
				connection.close();
			} catch (SQLException ex) {
				FileLogger.log("DBConnection.closeConnection()", ex.getMessage());
			}
		}
	}
	
	public static void closeConnection(final Connection connection, final Statement statement) {
		try {
			statement.close();
		} catch (SQLException e) {
			FileLogger.log("DBConnection.closeConnection() - statement", e.toString());
		}
		try {
			connection.close();
		} catch (SQLException e) {
			FileLogger.log("DBConnection.closeConnection() - connection", e.toString());
		}
	}
}
